package com.qfedu.service.impl;

import com.qfedu.dao.OrderRepository;
import com.qfedu.dao.OrderMapper;
import com.qfedu.pojo.entity.Order;
import com.qfedu.pojo.entity.Orders;
import com.qfedu.pojo.req.OrderReq;
import com.qfedu.pojo.req.OrdersReq;
import com.qfedu.pojo.resp.BaseResp;
import com.qfedu.pojo.resp.OrderResp;
import com.qfedu.service.OrderService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:User
 * @time:2022/9/28
 * @explain:
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;


    @Autowired
    private OrderMapper orderMapper;
    /**
     * 添加订单项
     * @param orderReq
     * @return
     */
    @Override
    public BaseResp addOrder(OrderReq orderReq) {
        Order order = new Order();
        BeanUtils.copyProperties(orderReq,order);
        orderMapper.addOrder(order);
        return new BaseResp().Success();

    }

    /**
     * 修改订单项
     * @param orderReq
     * @return
     */
    @Override
    public BaseResp updateOrder(OrderReq orderReq) {
        Order order = new Order();
        BeanUtils.copyProperties(orderReq,order);
        orderMapper.updateOrder(order);
        return  new BaseResp().Success();
    }

    /**
     * 删除订单项
     * @param orderReq
     * @return
     */
    @Override
    public BaseResp delOrder(OrderReq orderReq) {
        Order order = new Order();
        BeanUtils.copyProperties(orderReq,order);
       orderMapper.delOrder(order.getOrderItem());
        return new BaseResp().Success();
    }

    /**
     * 根据订单id查询订单项
     * @param orderId
     * @return
     */
    @Override
    public BaseResp findAll(String orderId) {
        System.out.println("order = " + orderId);
        List<Order> byOrderId = orderRepository.findByOrderId(orderId);
//        List<OrderResp> orderResp = new ArrayList<>();
//        for (Order  o:byOrdersId){
//          OrderResp  orderResp1 = new OrderResp();
//          BeanUtils.copyProperties(o,orderResp1);
//          orderResp.add(orderResp1);
//
//        }
        return new BaseResp(1,"SUCCESS",byOrderId,null);
    }
}
